package algorithm.ks;

import java.util.*;

public class Main {


    //股票
    public double get_max_profit (double M, int N, double[] historyPrices, int K) {
        // write code here
        return process(M, N, historyPrices, 0, K, 0, M);
    }

    public double process(double M, int N, double[] historyPrices, int index, int restK, int num, double money){
        if(restK == 0)
            return money - M;
        if(index == N - 1)
            return historyPrices[index] * num -M;
        double max = Double.MIN_VALUE;
        if(num == 0){
            double ma = process(M, N, historyPrices, index + 1, restK, (int) (money / historyPrices[index]), 0);
            double no_ma = process(M, N, historyPrices, index + 1, restK, num, money);
            max = Math.max(ma, no_ma);
        }else {
            double ma = process(M, N, historyPrices, index + 1, restK - 1, 0, num * historyPrices[index]);
            double no_ma = process(M, N, historyPrices, index + 1, restK, num, money);
            max = Math.max(ma, no_ma);
        }
        return max;
    }



    public int[] constructArr (int[] a) {
        int[] result = new int[a.length];
        int[] tmp1 = new int[a.length];
        int[] tmp2 = new int[a.length];
        int s = 1;
        for (int i = 0; i < a.length; i++) {
            s = s * a[i];
            tmp1[i] = s;
        }
        for (int i = a.length - 1; i >= 0; i--) {
            s = s * a[i];
            tmp1[i] = s;
        }

        for (int i = 0; i < a.length; i++) {
            int before = i == 0 ? 1 : tmp1[i - 1];
            int after = i == a.length - 1 ? 1 : tmp2[i + 1];
            result[i] = before * after;
        }
        return result;
        // write code here
    }

    static class Node {
        Set<Integer> branchNodes = new HashSet<>();
        int value;
        public Node(int value){
            this.value = value;
        }
    }

    public static int getLength(int node, Node[] tree){
        if(tree[node] == null)
            return 0;
        Node head = tree[node];
        int max = Integer.MIN_VALUE;

        for (Integer branchNode : head.branchNodes) {
            max = Math.max(max, getLength(branchNode, tree) + 1);
        }
        return max;
    }
    public static void main(String[] args) {
//        double s[] = {1.0, 2.0, 1.0, 2.0, 2.0, 3.0, 2.0};
//        double max_profit = new Main().get_max_profit(10000, 7, s, 2);
//        System.out.println(max_profit);

        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        Node[] tree = new Node[num + 1];
        Integer[] table = new Integer[num + 1];
        for (int i = 1; i < table.length; i++) {
            table[i] = i;
        }
        for (int i = 1; i <= num - 1; i++) {
            int value = sc.nextInt();
            Node node = null;
            if(tree[value] == null){
                node = new Node(value);
                tree[value] = node;
            }
            else
                node = tree[value];
            node.branchNodes.add(table[i + 1]);
        }
        int node1 = sc.nextInt();
        int node2 = sc.nextInt();
        if (tree[node1]!=null && tree[node1].branchNodes.contains(table[node2])) {
            int length1 = getLength(node2, tree);
            tree[node1].branchNodes.remove(table[node2]);
            int length2 = getLength(node1, tree);
            System.out.println(length2 + length1 + 1);
        } else if(tree[node2]!=null) {
            int length1 = getLength(node1, tree);
            tree[node2].branchNodes.remove(table[node1]);
            int length2 = getLength(node2, tree);
            System.out.println(length2 + length1 + 1);
        }


//        for (int i = num - 1; i < num && i > 10; i--) {
//            int ws = 0;
//            for(int tmp = i; tmp > 0; tmp = tmp / 10){
//                ws++;
//            }
//            int result = 0;
//            int tmp = i;
//            for (int j = 0; j < ws; j++) {
//                result += Math.pow(tmp % 10, ws);
//                tmp = tmp / 10;
//            }
//            if(result == i)
//                System.out.println(i);
//        }
//        for (int i = 0; i < length; i++) {
//            int num = sc.nextInt();
//            int k = sc.nextInt();
//            int[] list = new int[num];
//            int[] tmp = new int[num];
//            for (int j = 0; j < num; j++) {
//                list[j] = sc.nextInt();
//                tmp[j] = list[j];
//            }
//            Arrays.sort(list);
//            int index = 0;
//            boolean flag = true;
//            int min = Integer.MAX_VALUE;
//            for (int j = 0; j < num; j++) {
//                if(list[j] == tmp[j] && flag){
//                    index++;
//
//                    min = list[j + 1];
//                } else if (tmp[j] < (min)) {
//                    index++;
//                    min = tmp[j];
//                    flag = false;
//                }
//            }
//            int result = (num - index) % k == 0? (num - index) / k : 1 + (num - index) / k;
//            System.out.println(result);
//        }


//        int[][] data1 = new int[row][col];
//        int[][] data2 = new int[row][col];
//        int result1 = 0;
//        for (int i = 0; i < row; i++) {
//            char[] chars = sc.next().toCharArray();
//            for (int j = 0; j < col; j++) {
//                data1[i][j] = chars[0] == 'R'? 0 : 1;
//                data2[i][j] = chars[0] == 'R'? -1 : chars[0] == 'G' ? 0 : 1;
//            }
//        }
//
//        for (int i = 0; i < row; i++) {
//            for (int j = 0; j < ; j++) {
//
//            }
//        }


    }

//    public static void process(int row, int col, int[][] data){
//        if(col > 0 && data[row][col - 1] != -1){
//            data[row][col - 1] = -1;
//            process(row, col - 1, data);
//        }
//        if(col < data[0].length - 1 && data[row][col + 1] != -1){
//            data[row][col + 1] = -1;
//            process(row, col + 1, data);
//        }
//        if(row > 0 && data[row - 1][col] != -1){
//            data[row - 1][col] = -1;
//            process(row - 1, col, data);
//        }
//        if(row < data.length - 1 && data[row + 1][col] != -1){
//            data[row + 1][col] = -1;
//            process(row + 1, col, data);
//        }
//    }
}
